package com.bootdo.system.service.impl;

import com.bootdo.common.domain.DictDO;
import com.bootdo.common.utils.DictUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.IntegralLogDao;
import com.bootdo.system.dao.JournalDao;
import com.bootdo.system.dao.UserDao;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.IntegralLogDO;
import com.bootdo.system.domain.JournalDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.IntegralLogService;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Transactional
@Service
public class IntegralLogServiceImpl implements IntegralLogService {

	@Autowired
	private IntegralLogDao logDao;

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private JournalDao journalDao;
	
	@Override
	public IntegralLogDO get(Long id){
		return logDao.get(id);
	}
	
	@Override
	public List<IntegralLogDO> list(Map<String, Object> map){
		return logDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return logDao.count(map);
	}
	
	@Override
	public R save(IntegralLogDO log){
		float giveRate = 0;
		//获取赠送人用户角色（普通用户赠送，需要扣除手续费，管理员赠送，不需要）
		List<Long> list = userRoleDao.listRoleId(log.getFromUserId());
		//获取赠送扣除积分比率
		DictDO dictDO = DictUtils.getDictByType("give_rate");
		if(null == dictDO) {
			return R.error("赠送积分扣除比率不能为空");
		}
		giveRate = Float.parseFloat(dictDO.getValue());
		//判断赠送人积分是否充足
		UserDO fromUserDO = userDao.get(log.getFromUserId());
		//计算所需的积分
		BigDecimal total = new BigDecimal(0);
		if(giveRate != 0) {
			BigDecimal rate = new BigDecimal(giveRate / 100);
			total = new BigDecimal(log.getIntegral()).multiply(rate);
		}else {
			 total = new BigDecimal(log.getIntegral());
		}
		Long number = Math.round(total.doubleValue());
		long total1 = number + log.getIntegral();

		//获取赠送人用户角色（普通用户赠送，需要扣除积分，管理员赠送，不需要扣除本身积分）
		if(!CollectionUtils.isEmpty(list)) {
			if(!list.contains(1l)) {
				if(StringUtils.equals("1",fromUserDO.getFrozenType())) {
					return R.error("赠送人积分账号已冻结");
				}
				//普通用户
				if(total1 > fromUserDO.getIntegral()) {
					return R.error("赠送人积分不足");
				}
				//积分余额
				log.setAllIntegral(fromUserDO.getIntegral() - total1);
			}else {
				//积分余额
				log.setAllIntegral(fromUserDO.getIntegral());
			}
		}
		log.setCreateBy(log.getFromUserId());
		log.setCreateDate(new Date());
		log.setDelFlag("0");
		log.setIntegralDescription("赠送人赠送扣除" + log.getIntegral() + "积分,手续费扣除积分为" + number + "积分,接收人获得 " + log.getIntegral() + "积分");
		//保存赠送记录
		logDao.save(log);

		//保存积分流水
		JournalDO journalDO1 = new JournalDO();
		journalDO1 = setData(fromUserDO);
		journalDO1.setJournalType("1");
		journalDO1.setJournalDescription("赠送人扣除" + log.getIntegral() + "积分,扣除赠送手续费" + number + "积分");
		journalDO1.setIntegral(total1);
		journalDO1.setServiceIntegral(number);
		journalDO1.setAllIntegral(log.getAllIntegral());
		journalDao.save(journalDO1);

		//获取赠送人用户角色（普通用户赠送，需要扣除积分，管理员赠送，不需要扣除本身积分）
		if(!CollectionUtils.isEmpty(list)) {
			if(!list.contains(1l)) {
				//普通用户
				//赠送人扣除积分
				fromUserDO.setIntegral(total1);
				userDao.reduceIntegral(fromUserDO);
			}
		}
		//接收人增加积分
		UserDO toUserDO = new UserDO();
		toUserDO.setUserId(log.getToUserId());
		toUserDO.setIntegral(log.getIntegral());
		userDao.addIntegral(toUserDO);

		//保存积分流水
		toUserDO = userDao.get(toUserDO.getUserId());
		JournalDO journalDO2 = new JournalDO();
		journalDO2 = setData(toUserDO);
		journalDO2.setJournalType("6");
		journalDO2.setJournalDescription("接收人获得" + log.getIntegral() + "积分");
		journalDO2.setIntegral(log.getIntegral());
		journalDO2.setServiceIntegral(0l);
		journalDO2.setAllIntegral(fromUserDO.getIntegral() + log.getIntegral());
		journalDao.save(journalDO2);

		return R.ok();
	}

	private JournalDO setData(UserDO userDO) {
		JournalDO journalDO = new JournalDO();
		journalDO.setCreateBy(userDO.getUserId());
		journalDO.setUserId(userDO.getUserId());
		journalDO.setCreateDate(new Date());
		journalDO.setDelFlag("1");
		return journalDO;
	}

	@Override
	public int update(IntegralLogDO log){
		return logDao.update(log);
	}
	
	@Override
	public int remove(Long id){
		return logDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return logDao.batchRemove(ids);
	}
	
}
